//axios的配置
import axios from "axios";
import {MessageBox,Message} from "element-ui";
import nprogress from 'nprogress'
//对axios进行一些简单的配置
//axios的公共配置
let http = axios.create({
    withCredentials: true,//是否携带凭证
    baseURL: '/api',//基础地址
    timeout: 5000,//超时设置

})

//请求拦截
http.interceptors.request.use(config=>{
    nprogress.start();
    // console.log(111)
    //config就是请求的对象，里面可以拿到请求的地址
    let token = localStorage.getItem('wqj-token')
    config.headers.authorization = token
    //return config 理解为放行
    return config

})

//响应拦截
http.interceptors.response.use(config=>{
    nprogress.done()
    // console.log(222)
    //config是响应对象，里面可以拿到服务器响应的内容

    //return config 就是放行
    // console.log(config);
    let {data:{ state, msg, code }} = config;
    let codes = ['10022','1004','10026']
    if(codes.includes(code)){

        localStorage.removeItem('wqj-token')

        location.reload()
    }
    // console.log(state);
    //一个需要排除掉不弹窗的接口
    let excluderUrl = ['/order/query_order_status']
    let isExclude = excluderUrl.includes(config.config.url)

    if(!state && !isExclude){
        MessageBox.alert(msg,'错误提示',{type:"error"})
    }
    return config

});

export default http;